<template>
  <div>
    <el-table border ref="paramsTable" stripe :data="TableData">
      <el-table-column type="expand" prop="attr_vals">
        <template v-slot="scope">
          <el-tag
            closable
            v-for="(item, index) in scope.row.attr_vals"
            :key="index"
            @close="handleClose(scope.row,item)"
            >{{ item }}</el-tag
          >
          <el-input
            class="input-new-tag"
            v-if="scope.row.inputVisible"
            v-model="scope.row.inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="handleInputConfirm(scope.row)"
            @blur="handleInputConfirm(scope.row)"
          >
          </el-input>
          <el-button
            v-else
            class="button-new-tag"
            size="small"
            @click="showInput(scope.row)"
            >+ New Tag</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="#" type="index"></el-table-column>
      <el-table-column
        :label="thisStatus ? '参数名称' : '属性名称'"
        prop="attr_name"
      ></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            type="primary"
            @click="changeParams(scope.row)"
            icon="el-icon-edit"
            size="mini"
            >编辑</el-button
          >
          <el-button
            type="danger"
            @click="removeParams(scope.row.attr_id)"
            icon="el-icon-delete"
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <changeParamsDialog
      @getParams="getParams"
      ref="changeParamsDialog"
    ></changeParamsDialog>
  </div>
</template>

<script>
import { putParamsTagApi, delParamsDataApi } from '@/Utils/Api/gitGoodsInfoApi'
import changeParamsDialog from './changeParamsDialog.vue'
export default {
  name: 'paramsTable',
  props: {
    thisStatus: Boolean,
    cateId: Number
  },
  data () {
    return {
      TableData: []
    }
  },
  components: {
    changeParamsDialog
  },
  created () {},
  mounted () {},
  methods: {
    // 修改标签
    async putParamsTag (row) {
      const { data } = await putParamsTagApi(row.cat_id, row.attr_id, {
        attr_sel: row.attr_sel,
        attr_name: row.attr_name,
        attr_vals: row.attr_vals.join(' ')
      })
      if (data.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: data.meta.msg
        })
      }
      this.$message({
        type: 'success',
        message: data.meta.msg
      })
    },
    async handleClose (row, tag) {
      const confirmResult = await this.$confirm('此操作将永久删除该分类, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除操作!')
      }
      console.log(row.attr_vals)
      row.attr_vals.splice(row.attr_vals.indexOf(tag), 1)
      this.putParamsTag(row)
    },
    showInput (row) {
      row.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm (row) {
      if (row.inputValue.trim().length === 0) {
        row.inputVisible = false
        row.inputValue = ''
        return
      }
      row.attr_vals.push(row.inputValue.trim())
      this.putParamsTag(row)
      row.inputVisible = false
      row.inputValue = ''
    },
    async removeParams (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该分类, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message({
          type: 'info',
          message: '用户取消了删除'
        })
      }
      this.delParamsData(id)
      this.getParams()
    },
    // 删除商品参数
    async delParamsData (id) {
      const { data } = await delParamsDataApi(this.cateId, id)
      console.log(data)
      if (data.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: data.meta.msg
        })
      }
      this.$message({
        type: 'success',
        message: data.meta.msg
      })
    },
    changeParams (row) {
      this.$nextTick(() => {
        this.$refs.changeParamsDialog.dialogVisible = true
        this.$refs.changeParamsDialog.attrId = row.attr_id
        this.$refs.changeParamsDialog.cateId = this.cateId
        this.$refs.changeParamsDialog.viewStatus = this.thisStatus
        this.$refs.changeParamsDialog.activeName = row.attr_sel
      })
    },
    getParams () {
      this.$emit('getParams')
    }
  },
  computed: {},
  watch: {}
}
</script>

<style lang="less" scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
